// MyFamily - XML based genealogy program.
// Copyright (C) 2006  Doug Swisher
// See the file COPYING.txt for license details.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DataModel
{
    public class GedComReader : IDisposable
    {
        private int lineNumber;
        private GedComLine currentLine;
        private StreamReader reader;
        private bool eof;


        public GedComReader(FileInfo info)
        {
            reader = info.OpenText();
            Advance();
        }


        public GedComLine CurrentLine
        {
            get { return this.currentLine; }
        }

        internal void Advance()
        {
            this.currentLine = null;
            while (!this.eof && this.currentLine == null)
            {
                string line = reader.ReadLine();

                if (line == null)
                {
                    this.eof = true;
                    this.currentLine = null;
                }
                else
                {
                    this.lineNumber++;
                    this.currentLine = GedComLine.Parse(line, this.lineNumber);
                }
            }
        }


        public bool Eof
        {
            get { return this.eof; }
        }



        #region IDisposable Members

        public void Dispose()
        {
            if (this.reader != null)
            {
                this.reader.Close();
                this.reader = null;
            }
        }

        #endregion
    }
}
